package com.example.multiactivity

import android.app.Activity
import android.app.ComponentCaller
import android.content.Intent
import android.os.Bundle
import android.widget.EditText
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

object ActivityMsg {
    const val FIRST_ACTIVITY = "FIRST_MSG"
    const val SECOND_ACTIVITY = "SECOND_MSG"
}

private lateinit var receiveText: TextView; // 全局变量

class MainActivity : AppCompatActivity() {
    // 注册回调: 接收 third activity 传递过来的数据
    private val getResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            result ->
        if (result.resultCode == Activity.RESULT_OK) {
            val data = result.data
            val value = data?.getStringExtra("resultKey")
            receiveText.setText(value);
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // 点击 main_activity 的 button 时跳转 second_activity
        val editText = findViewById<EditText>(R.id.mainEditText)
        val mainButton = findViewById<Button>(R.id.mainButton)
        receiveText = findViewById<TextView>(R.id.mainReceiveTextView);

        // 监听按钮点击
        mainButton.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            intent.putExtra("Main-key", editText.text.toString()); // Main 传递 数据给 B
            startActivity(intent);
        }

        // 点击 second_activity 的 button 时跳转 third_activity
        val secondButton = findViewById<Button>(R.id.secondButton)

        // 监听按钮点击
        secondButton.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java);
            getResult.launch(intent); // 使用launch才能实现数据的回传
        }

        val thirdButton = findViewById<Button>(R.id.thirdButton)

        // 监听按钮点击
        thirdButton.setOnClickListener {
            val intent = Intent(this, FourthActivity::class.java)
//            intent.putExtra("Main-key", editText.text.toString()); // Main 传递 数据给 B
            startActivity(intent);
        }

        val fourthButton = findViewById<Button>(R.id.fourthButton)

        // 监听按钮点击
        fourthButton.setOnClickListener {
            val intent = Intent(this, fifthActivity::class.java)
//            intent.putExtra("Main-key", editText.text.toString()); // Main 传递 数据给 B
            startActivity(intent);
        }

    }


}